package cn.iocoder.yudao.module.game.controller.admin.agentTeam;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.agentTeam.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.agentTeam.AgentTeamDO;
import cn.iocoder.yudao.module.game.service.agentTeam.AgentTeamService;

@Tag(name = "管理后台 - 代理团队")
@RestController
@RequestMapping("/game/agent-team")
@Validated
public class AgentTeamController {

    @Resource
    private AgentTeamService agentTeamService;

    @PostMapping("/create")
    @Operation(summary = "创建代理团队")
    @PreAuthorize("@ss.hasPermission('game:agent-team:create')")
    public CommonResult<Long> createAgentTeam(@Valid @RequestBody AgentTeamSaveReqVO createReqVO) {
        return success(agentTeamService.createAgentTeam(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新代理团队")
    @PreAuthorize("@ss.hasPermission('game:agent-team:update')")
    public CommonResult<Boolean> updateAgentTeam(@Valid @RequestBody AgentTeamSaveReqVO updateReqVO) {
        agentTeamService.updateAgentTeam(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除代理团队")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:agent-team:delete')")
    public CommonResult<Boolean> deleteAgentTeam(@RequestParam("id") Long id) {
        agentTeamService.deleteAgentTeam(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得代理团队")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:agent-team:query')")
    public CommonResult<AgentTeamRespVO> getAgentTeam(@RequestParam("id") Long id) {
        AgentTeamDO agentTeam = agentTeamService.getAgentTeam(id);
        return success(BeanUtils.toBean(agentTeam, AgentTeamRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得代理团队分页")
    @PreAuthorize("@ss.hasPermission('game:agent-team:query')")
    public CommonResult<PageResult<AgentTeamRespVO>> getAgentTeamPage(@Valid AgentTeamPageReqVO pageReqVO) {
        PageResult<AgentTeamDO> pageResult = agentTeamService.getAgentTeamPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AgentTeamRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出代理团队 Excel")
    @PreAuthorize("@ss.hasPermission('game:agent-team:export')")
    @OperateLog(type = EXPORT)
    public void exportAgentTeamExcel(@Valid AgentTeamPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AgentTeamDO> list = agentTeamService.getAgentTeamPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "代理团队.xls", "数据", AgentTeamRespVO.class,
                        BeanUtils.toBean(list, AgentTeamRespVO.class));
    }

}